package com.hinotoyk.testsystem.uitls;

import java.util.Random;

public class ChainRandom {
    private static Random r = new Random();
    private int end = 0;
    private int count = 0;
    private boolean chain [] = null;

    public boolean allTrue(){
        if(chain == null) return false;

        for(int i = 0; i < chain.length; ++i)
            if(chain[i] == false)
                return false;

        return true;
    }

    public void setChain(int end){
        if(this.end != end && end > 0) {
            this.end = end;
            if(chain == null || chain.length < end)
                chain = new boolean[end];
        }

        this.resetChain();
    }

    public void resetChain(){
        count = 0;
        if(chain != null)
            for(int i = 0; i < chain.length; ++i)
                chain[i] = false;
    }

    public int nextInt(){
        if(end == 0 || chain == null)
            return r.nextInt();

        if(count >= end)
            resetChain();

        int ret = r.nextInt(end);

        while(chain[ret]){
            ret = r.nextInt(end);
        }

        chain[ret] = true;
        ++count;

        return ret;
    }

    public int nextInt(int end){
        if(this.end != end){
            this.setChain(end);
        }

        return nextInt();
    }
}
